
package com.hlkj.pay.app.order.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.pay.app.merchant.impl.CommonChannelCallHandler;
import com.hlkj.pay.app.order.IRefundOrderAppService;
import com.hlkj.pay.app.transfer.PayOrderDoToResp;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.common.OpenApiResultCode;
import com.hlkj.pay.common.PayResultCode;
import com.hlkj.pay.dto.LocalMerchantRequest;
import com.hlkj.pay.dto.order.RefundOrderChannelResultDto;
import com.hlkj.pay.dto.order.RefundOrderQueryDto;
import com.hlkj.pay.dto.order.notify.RefundOrderNotifyDto;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.gateway.order.IChannelRefundOrderGateway;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.infrastructure.model.order.RefundOrderDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.service.order.IPayOrderService;
import com.hlkj.pay.service.order.IRefundOrderService;
import com.hlkj.pay.service.pay.IProviderService;
import com.hlkj.pay.util.OrderCodeUtil;
import com.hlkj.pay.vo.order.req.RefundOrderParamReq;
import com.hlkj.pay.vo.order.req.RefundOrderQueryReq;
import com.hlkj.pay.vo.order.resp.RefundOrderResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/10/12 9:48
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RefundOrderAppServiceImpl implements IRefundOrderAppService {

    private final IPayOrderService orderService;

    private final IRefundOrderService refundOrderService;

    private final OrderHandler orderHandler;

    private final OrderCodeUtil orderCodeUtil;

    private final IProviderService providerService;

    private final IMerchantApplicationService merchantApplicationService;

    private final CommonChannelCallHandler commonChannelCallHandler;

    private final IChannelRefundOrderGateway channelRefundOrderGateway;

    @Override
    public CommonResult<RefundOrderResp> refundOrder(RefundOrderParamReq refundOrderParamReq) {
        if (!StringUtils.hasText(refundOrderParamReq.getPayOrderNo()) && !StringUtils.hasText(refundOrderParamReq.getMchOrderNo())) {
            return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
        }
        PayOrderDO payOrderDO = null;
        if (StringUtils.hasText(refundOrderParamReq.getPayOrderNo())) {
            payOrderDO = orderService.queryPayOrder(refundOrderParamReq.getPayOrderNo());
        }
        else {
            String sn = LocalContext.get().getSn();
            payOrderDO = orderService.queryPayOrderByMerOrderNo(sn, refundOrderParamReq.getMchOrderNo());
        }
        if (payOrderDO == null) {
            return CommonResult.error(OpenApiResultCode.MERCHANT_PAY_ORDER_NOT_EXIST);
        }
        boolean refundApply = channelRefundOrderGateway.refundApply(payOrderDO);
        if (!refundApply) {
            return CommonResult.error(OpenApiResultCode.MERCHANT_REFUND_NOT_SUPPORT);
        }

        BigDecimal subtract = payOrderDO.getAmount().subtract(payOrderDO.getRefundAmount());
        // 判断可退款金额
        if (subtract.compareTo(new BigDecimal(refundOrderParamReq.getAmount())) < 0) {
            return CommonResult.error(OpenApiResultCode.MERCHANT_REFUND_AMOUNT_ERROR);
        }
        // 获取最近的退款记录
        RefundOrderDO lastRefundOrderDO = refundOrderService.queryRefundLastOne(payOrderDO.getPayOrderNo());
        // 判断可退款金额
        if (lastRefundOrderDO != null) {
            PayOrderEnums.REFUND_STATUS refundStatus = PayOrderEnums.REFUND_STATUS.from(lastRefundOrderDO.getRefundStatus());
            switch (refundStatus) {
                case INIT:
                case REFUNDING:
                    return CommonResult.error(MerchantResultCode.MERCHANT_REFUNDING_ERROR);
            }

        }
        // 订单号生成
        String refundOrderNo = orderCodeUtil.orderNo(PayOrderEnums.ORDER_NO_TYPE.REFUND);
        if (!StringUtils.hasText(refundOrderNo)) {
            return CommonResult.error(MerchantResultCode.MERCHANT_REFUND_ORDER_NO_ERROR);
        }
        RefundOrderDO refundOrderDO = buildRefundOrder(lastRefundOrderDO, payOrderDO, refundOrderParamReq, refundOrderNo);
        boolean extensionExist = channelRefundOrderGateway.extensionExist(payOrderDO.getChannelCode());
        if (!extensionExist) {
            return CommonResult.error(CommonResultCode.EXTENSION_NOT_EXIST);
        }
        // 发起退款
        RefundOrderChannelResultDto refundOrderChannelResultDto = channelRefundOrderGateway.submitChannelRefundOrder(payOrderDO, refundOrderDO);
        // 返回参数填充
        buildThirdOrderParams(refundOrderDO, refundOrderChannelResultDto);
        // 非处理成功
        if (!refundOrderChannelResultDto.isProcessStatus()) {
            refundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.FAIL.getCode());
            refundOrderDO.setBankMsg(refundOrderChannelResultDto.getRemark());
        }
        // refundOrderService.addRefundOrder(refundOrderDO);
        addRefundOrder(refundOrderDO);
        if (!refundOrderChannelResultDto.isProcessStatus()) {
            return CommonResult.error(MerchantResultCode.MERCHANT_PAY_ORDER_FAIL.getCode(), refundOrderChannelResultDto.getRemark());
        }
        RefundOrderResp refundOrderResp = PayOrderDoToResp.INSTANCE.refundOrderResp(refundOrderDO);
        refundOrderResp.setStatus(refundOrderDO.getRefundStatus());

        orderHandler.sendOrderStatus(4, refundOrderDO.getRefundOrderNo(), PayOrderEnums.ORDER_STATUS_TYPE.REFUND_STATUS);
        // 返回退款数据
        return CommonResult.success(refundOrderResp);
    }

    @Override
    public CommonResult<RefundOrderResp> queryRefundOrder(RefundOrderQueryReq refundOrderQueryReq) {
        if (!StringUtils.hasText(refundOrderQueryReq.getRefundOrderNo()) && !StringUtils.hasText(refundOrderQueryReq.getMchRefundOrderNo())) {
            return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
        }
        RefundOrderDO refundOrderDO = null;
        if (StringUtils.hasText(refundOrderQueryReq.getRefundOrderNo())) {
            refundOrderDO = refundOrderService.queryRefundOrder(refundOrderQueryReq.getRefundOrderNo());
        }
        else {
            String sn = LocalContext.get().getSn();
            refundOrderDO = refundOrderService.queryRefundOrderByMerOrderNo(sn, refundOrderQueryReq.getMchRefundOrderNo());
        }
        if (refundOrderDO == null) {
            return CommonResult.error(MerchantResultCode.MERCHANT_REFUND_ORDER_NOT_EXIST);
        }
        // 支付中的订单需要请求通道 获取最新的结果
        queryRefundStatus(refundOrderDO);
        RefundOrderResp refundOrderResp = PayOrderDoToResp.INSTANCE.refundOrderResp(refundOrderDO);
        // 返回退款数据
        return CommonResult.success(refundOrderResp);

    }

    @Override
    public String refundCallBack(String channelCode, String providerCode, HttpServletRequest request) {
        // 获取通道配置
        ProviderChannelConfigDO providerChannelConfigDO = providerService.queryConfig(providerCode, channelCode);
        if (providerChannelConfigDO == null) {
            return processRefundCallBackErrorResponse(channelCode, providerCode, null, null, CommonResult.error(PayResultCode.PROVIDER_CHANNEL_NOT_EXITS));
        }
        CommonResult<RefundOrderNotifyDto> commonResult = channelRefundOrderGateway.refundStatusParse(request, providerChannelConfigDO);
        RefundOrderNotifyDto refundOrderNotifyDto = commonResult.getData();
        RefundOrderDO refundOrderDO = refundOrderNotifyDto.getRefundOrder();
        if (!commonResult.isSuccess()) {
            if (MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode().equals(commonResult.getCode())) {
                RefundOrderDO updatePayOrderDO = refundOrderDO;
                if (updatePayOrderDO != null && StringUtils.hasText(updatePayOrderDO.getPayOrderNo())
                        && PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(updatePayOrderDO.getRefundStatus())) {
                    RefundOrderDO refundOrderDb = refundOrderService.queryRefundOrder(updatePayOrderDO.getRefundOrderNo());
                    if (refundOrderDb == null) {
                        log.info("收到退款订单回调,签名错误,通过回调退款订单查询订单数据不存在 refundOrderNo:{}", updatePayOrderDO.getRefundOrderNo());
                        return commonResult.getMsg();
                    }
                    queryRefundStatus(refundOrderDb);
                    return processRefundCallBackSuccessResponse(channelCode, refundOrderDb, refundOrderNotifyDto.getCallBackBody(), CommonResult.success());
                }
                else {
                    return commonResult.getMsg();
                }
            }
            return commonResult.getMsg();
        }
        RefundOrderDO orderDO = refundOrderService.queryRefundOrder(refundOrderDO.getRefundOrderNo());
        if (orderDO == null) {
            return processRefundCallBackErrorResponse(channelCode, providerCode, refundOrderDO.getRefundOrderNo(), refundOrderNotifyDto.getCallBackBody(),
                    CommonResult.error(MerchantResultCode.MERCHANT_PAY_ORDER_NOT_EXIST));
        }
        if (orderDO.getRefundStatus().equals(refundOrderDO.getRefundStatus())) {
            return processRefundCallBackSuccessResponse(channelCode, orderDO, refundOrderNotifyDto.getCallBackBody(), CommonResult.success());
        }
        // 更新数据
        updateRefundOrder(refundOrderDO, orderDO);
        return processRefundCallBackSuccessResponse(channelCode, orderDO, refundOrderNotifyDto.getCallBackBody(), CommonResult.success());
    }

    @Override
    public String refundCallBack(String channelCode, String providerCode, String mchChannelCode, HttpServletRequest request) {
        // 获取通道配置
        ProviderChannelConfigDO providerChannelConfigDO = new ProviderChannelConfigDO();
        MerchantAppChannelDO merchantAppChannelDO = merchantApplicationService.queryAppChannelDetail(mchChannelCode);
        if(merchantAppChannelDO != null){
            providerChannelConfigDO.setParamsConfig(merchantAppChannelDO.getAppConfigJson());
            providerChannelConfigDO.setChannelCode(channelCode);
        }else{
            providerChannelConfigDO = providerService.queryConfig(providerCode, channelCode);
        }
        CommonResult<RefundOrderNotifyDto> commonResult = channelRefundOrderGateway.refundStatusParse(request, providerChannelConfigDO);
        RefundOrderNotifyDto refundOrderNotifyDto = commonResult.getData();
        RefundOrderDO refundOrderDO = refundOrderNotifyDto.getRefundOrder();
        if (!commonResult.isSuccess()) {
            if (MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode().equals(commonResult.getCode())) {
                RefundOrderDO updatePayOrderDO = refundOrderDO;
                if (updatePayOrderDO != null && StringUtils.hasText(updatePayOrderDO.getPayOrderNo())
                        && PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(updatePayOrderDO.getRefundStatus())) {
                    RefundOrderDO refundOrderDb = refundOrderService.queryRefundOrder(updatePayOrderDO.getRefundOrderNo());
                    if (refundOrderDb == null) {
                        log.info("收到退款订单回调,签名错误,通过回调退款订单查询订单数据不存在 refundOrderNo:{}", updatePayOrderDO.getRefundOrderNo());
                        return commonResult.getMsg();
                    }
                    queryRefundStatus(refundOrderDb);
                    return processRefundCallBackSuccessResponse(channelCode, refundOrderDb, refundOrderNotifyDto.getCallBackBody(), CommonResult.success());
                }
                else {
                    return commonResult.getMsg();
                }
            }
            return commonResult.getMsg();
        }
        RefundOrderDO orderDO = refundOrderService.queryRefundOrder(refundOrderDO.getRefundOrderNo());
        if (orderDO == null) {
            return processRefundCallBackErrorResponse(channelCode, providerCode, refundOrderDO.getRefundOrderNo(), refundOrderNotifyDto.getCallBackBody(),
                    CommonResult.error(MerchantResultCode.MERCHANT_PAY_ORDER_NOT_EXIST));
        }
        if (orderDO.getRefundStatus().equals(refundOrderDO.getRefundStatus())) {
            return processRefundCallBackSuccessResponse(channelCode, orderDO, refundOrderNotifyDto.getCallBackBody(), CommonResult.success());
        }
        // 更新数据
        updateRefundOrder(refundOrderDO, orderDO);
        return processRefundCallBackSuccessResponse(channelCode, orderDO, refundOrderNotifyDto.getCallBackBody(), CommonResult.success());
    }

    String processRefundCallBackSuccessResponse(String channelCode, RefundOrderDO refundOrderDO, String callBackBody, CommonResult commonResult) {
        if (refundOrderDO != null) {
            LocalMerchantRequest localMerchantRequest = (LocalMerchantRequest) LocalContext.get();
            localMerchantRequest.setProviderCode(refundOrderDO.getProviderCode());
            localMerchantRequest.setChannelCode(refundOrderDO.getChannelCode());
            localMerchantRequest.setMchChannelCode(refundOrderDO.getMchChannelCode());
            localMerchantRequest.setSn(refundOrderDO.getSn());
            localMerchantRequest.setAppId(refundOrderDO.getAppId());
            LocalContext.set(localMerchantRequest);
        }
        return commonChannelCallHandler.processRefundLogAndResponse(channelCode, refundOrderDO.getProviderCode(), refundOrderDO.getRefundOrderNo(), callBackBody, commonResult);
    }

    String processRefundCallBackErrorResponse(String channelCode, String providerCode, String requestId, String callBackBody, CommonResult commonResult) {
        return commonChannelCallHandler.processRefundLogAndResponse(channelCode, providerCode, requestId, callBackBody, commonResult);
    }

    @Override

    public boolean queryRefundStatus(String refundOrderNo) {
        RefundOrderDO refundOrderDO = refundOrderService.queryRefundOrder(refundOrderNo);
        if (refundOrderDO == null) {
            return false;
        }
        return queryRefundStatus(refundOrderDO);
    }

    @Override
    @Async("taskExecutor")
    public void queryRefundStatusList(List<RefundOrderDO> refundingList) {
        log.info("退款状态查询");
        refundingList.stream().forEach(refundOrderDO -> {
            queryRefundStatus(refundOrderDO);
        });
    }

    @Override
    public void syncRefundOrderStatus() {
        log.info("退款订单状态同步");
        RefundOrderQueryDto refundOrderQueryDto = new RefundOrderQueryDto();
        refundOrderQueryDto.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUNDING.getCode());
        List<RefundOrderDO> refundingList = refundOrderService.queryList(refundOrderQueryDto);
        if (CollectionUtils.isEmpty(refundingList)) {
            return;
        }
        refundingList.stream().forEach(refundOrderDO -> {
            queryRefundStatus(refundOrderDO);
        });
    }

    /**
     * 查询退款状态 返回是否结束状态
     * 
     * @param refundOrderDO
     * @return
     */
    boolean queryRefundStatus(RefundOrderDO refundOrderDO) {
        // 支付中的订单需要请求通道 获取最新的结果
        if (PayOrderEnums.REFUND_STATUS.REFUNDING.getCode().equals(refundOrderDO.getRefundStatus())) {
            boolean extensionExist = channelRefundOrderGateway.extensionExist(refundOrderDO.getChannelCode());
            if (extensionExist) {
                RefundOrderChannelResultDto refundOrderChannelResultDto = channelRefundOrderGateway.queryChannelRefundOrder(refundOrderDO);
                RefundOrderDO updateRefundOrderDO = refundOrderChannelResultDto.getUpdateRefundOrderDO();
                if (updateRefundOrderDO != null) {
                    // 二者数据不一致 需要更新数据
                    if (!updateRefundOrderDO.getRefundStatus().equals(refundOrderDO.getRefundStatus())) {
                        // 更新数据
                        updateRefundOrder(updateRefundOrderDO, refundOrderDO);
                        // 更新后的熟悉进行复制
                        refundOrderDO.setRefundStatus(updateRefundOrderDO.getRefundStatus());
                        refundOrderDO.setBankCode(updateRefundOrderDO.getBankCode());
                        refundOrderDO.setBankMsg(updateRefundOrderDO.getBankMsg());
                        refundOrderDO.setSuccessTime(updateRefundOrderDO.getSuccessTime());
                        return false;
                    }
                    else {
                        return true;
                    }
                }
                else {
                    return true;
                }
            }
            else {
                log.info("退款订单 当前通道不支持渠道订单功能 channelCode:{}", refundOrderDO.getChannelCode());
                return false;
            }
        }
        return false;
    }

    private void updateRefundOrder(RefundOrderDO updateRefundOrderDO, RefundOrderDO refundOrderDO) {
        updateRefundOrderDO.setId(refundOrderDO.getId());
        if (PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(updateRefundOrderDO.getRefundStatus())) {
            if (updateRefundOrderDO.getSuccessTime() == null) {
                updateRefundOrderDO.setSuccessTime(System.currentTimeMillis());
            }
        }
        refundOrderService.updateRefundOrder(updateRefundOrderDO, refundOrderDO.getRefundOrderNo(), refundOrderDO.getSn(), refundOrderDO.getMchRefundOrderNo());
        // 下发状态变更通知
        if (PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(updateRefundOrderDO.getRefundStatus())
                || PayOrderEnums.REFUND_STATUS.FAIL.getCode().equals(updateRefundOrderDO.getRefundStatus())) {
            refundOrderDO.setRefundStatus(updateRefundOrderDO.getRefundStatus());
            if (PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(updateRefundOrderDO.getRefundStatus())) {
                // 更新订单退款信息
                PayOrderDO payOrderDO = orderService.queryPayOrder(refundOrderDO.getPayOrderNo());
                PayOrderDO updatePayOrderDO = new PayOrderDO();
                updatePayOrderDO.setId(payOrderDO.getId());
                updatePayOrderDO.setRefundAmount(refundOrderDO.getTotalRefundAmount());
                if (payOrderDO.getPayAmount() == null) {
                    updatePayOrderDO.setPayAmount(payOrderDO.getAmount());
                    payOrderDO.setPayAmount(payOrderDO.getAmount());
                }
                if (payOrderDO.getPayAmount().compareTo(refundOrderDO.getTotalRefundAmount()) != 0) {
                    updatePayOrderDO.setRefundStatus(PayOrderEnums.ORDER_REFUND_STATUS.PART_REFUND.getCode());
                }
                else {
                    updatePayOrderDO.setRefundStatus(PayOrderEnums.ORDER_REFUND_STATUS.REFUND.getCode());
                }
                log.info("退款完成 更新订单退款状态 payOrderNo:{},refundStatus:{}", payOrderDO.getPayOrderNo(), updatePayOrderDO.getRefundStatus());
                orderService.updatePayOrder(updatePayOrderDO, payOrderDO.getPayOrderNo(), payOrderDO.getOrderSn(), payOrderDO.getMchOrderNo());
            }
            orderHandler.refundOrder(refundOrderDO);
        }
    }

    private void addRefundOrder(RefundOrderDO refundOrderDO) {
        if (PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(refundOrderDO.getRefundStatus())) {
            if (refundOrderDO.getSuccessTime() == null) {
                refundOrderDO.setSuccessTime(System.currentTimeMillis());
            }
        }
        if (PayOrderEnums.REFUND_STATUS.REFUNDING.getCode().equals(refundOrderDO.getRefundStatus())){
            // 更新订单退款信息
            PayOrderDO payOrderDO = orderService.queryPayOrder(refundOrderDO.getPayOrderNo());
            PayOrderDO updatePayOrderDO = new PayOrderDO();
            updatePayOrderDO.setId(payOrderDO.getId());
            updatePayOrderDO.setRefundStatus(PayOrderEnums.ORDER_REFUND_STATUS.REFUNDING.getCode());
            orderService.updatePayOrder(updatePayOrderDO, payOrderDO.getPayOrderNo(), payOrderDO.getOrderSn(), payOrderDO.getMchOrderNo());
        }
        refundOrderService.addRefundOrder(refundOrderDO);
        // 下发状态变更通知
        if (PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(refundOrderDO.getRefundStatus())
                || PayOrderEnums.REFUND_STATUS.FAIL.getCode().equals(refundOrderDO.getRefundStatus())) {
            refundOrderDO.setRefundStatus(refundOrderDO.getRefundStatus());
            if (PayOrderEnums.REFUND_STATUS.REFUND.getCode().equals(refundOrderDO.getRefundStatus())) {
                // 更新订单退款信息
                PayOrderDO payOrderDO = orderService.queryPayOrder(refundOrderDO.getPayOrderNo());
                PayOrderDO updatePayOrderDO = new PayOrderDO();
                updatePayOrderDO.setId(payOrderDO.getId());
                updatePayOrderDO.setRefundAmount(refundOrderDO.getTotalRefundAmount());
                if (payOrderDO.getPayAmount() == null) {
                    updatePayOrderDO.setPayAmount(payOrderDO.getAmount());
                    payOrderDO.setPayAmount(payOrderDO.getAmount());
                }
                if (payOrderDO.getPayAmount().compareTo(refundOrderDO.getTotalRefundAmount()) != 0) {
                    updatePayOrderDO.setRefundStatus(PayOrderEnums.ORDER_REFUND_STATUS.PART_REFUND.getCode());
                }
                else {
                    updatePayOrderDO.setRefundStatus(PayOrderEnums.ORDER_REFUND_STATUS.REFUND.getCode());
                }
                log.info("退款完成 更新订单退款状态 payOrderNo:{},refundStatus:{}", payOrderDO.getPayOrderNo(), updatePayOrderDO.getRefundStatus());
                orderService.updatePayOrder(updatePayOrderDO, payOrderDO.getPayOrderNo(), payOrderDO.getOrderSn(), payOrderDO.getMchOrderNo());
            }
            orderHandler.refundOrder(refundOrderDO);
        }
    }

    // 生成退款订单对象
    RefundOrderDO buildRefundOrder(RefundOrderDO lastRefundOrderDO, PayOrderDO payOrderDO, RefundOrderParamReq refundOrderParamReq, String refundOrderNo) {
        RefundOrderDO refundOrderDO = new RefundOrderDO();
        refundOrderDO.setRefundOrderNo(refundOrderNo);
        refundOrderDO.setMchRefundOrderNo(refundOrderParamReq.getMchRefundOrderNo());
        refundOrderDO.setPayOrderNo(payOrderDO.getPayOrderNo());
        refundOrderDO.setMchOrderNo(payOrderDO.getMchOrderNo());
        refundOrderDO.setMchChannelCode(payOrderDO.getMchChannelCode());
        refundOrderDO.setSn(payOrderDO.getSn());
        refundOrderDO.setSubSn(payOrderDO.getOrderSn());
        refundOrderDO.setAppId(payOrderDO.getAppId());
        refundOrderDO.setProviderCode(payOrderDO.getProviderCode());
        refundOrderDO.setChannelCode(payOrderDO.getChannelCode());
        refundOrderDO.setPayWayCode(payOrderDO.getPayWayCode());
        // refundOrderDO.setChannelConfig();
        refundOrderDO.setRefundReason(refundOrderParamReq.getReason());
        refundOrderDO.setRefundAmount(new BigDecimal(refundOrderParamReq.getAmount()));
        refundOrderDO.setAmount(payOrderDO.getAmount());
        if (payOrderDO.getRefundAmount() != null) {
            refundOrderDO.setTotalRefundAmount(payOrderDO.getRefundAmount().add(refundOrderDO.getRefundAmount()));
        }
        else {
            refundOrderDO.setTotalRefundAmount(refundOrderDO.getRefundAmount());
        }
        // refundOrderDO.setActualRefundAmount();
        // refundOrderDO.setTotalRefundAmount();
        // refundOrderDO.setTotalFeeAmount();
        // refundOrderDO.setSuccessTime();
        // refundOrderDO.setBankCode();
        // refundOrderDO.setBankMsg();
        refundOrderDO.setNotifyUrl(refundOrderParamReq.getNotifyUrl());
        // refundOrderDO.setNotifyTime();
        // refundOrderDO.setNotifyState();
        // refundOrderDO.setSignType();
        refundOrderDO.setCreateTime(System.currentTimeMillis());
        return refundOrderDO;
    }

    void buildThirdOrderParams(RefundOrderDO refundOrderDO, RefundOrderChannelResultDto refundOrderChannelResultDto) {
        // 部分场景没有直接调用第三方 所有没有返回对应的第三方订单号
        RefundOrderDO updateRefundOrderDO = refundOrderChannelResultDto.getUpdateRefundOrderDO();
        if (updateRefundOrderDO != null) {
            refundOrderDO.setRefundStatus(updateRefundOrderDO.getRefundStatus());
            refundOrderDO.setBankCode(updateRefundOrderDO.getBankCode());
            refundOrderDO.setBankMsg(updateRefundOrderDO.getBankMsg());
            refundOrderDO.setSuccessTime(updateRefundOrderDO.getSuccessTime());
        }
        else {
            refundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUNDING.getCode());
        }
    }

}
